package cn.orange.ch01_array;

/**
 * LC283.移动零
 */
public class LC283 {
    public void moveZeroes(int[] nums) {
        int slow = 0;
        for (int fast = 0; fast < nums.length; fast++) {
            if (nums[fast] != 0) {
                nums[slow++] = nums[fast];
            }
        }
        while (slow < nums.length) {
            nums[slow++] = 0;
        }
    }

    //LC844
    public boolean backspaceCompare(String s, String t) {
        String s1 = getStr(s);
        String t1 = getStr(t);
        return s1.equals(t1);
    }

    private String getStr(String s) {
        StringBuilder sb = new StringBuilder();
        for (char c : s.toCharArray()) {
            if (c == '#') {
                if (!sb.isEmpty()) {
                    sb.deleteCharAt(sb.length() - 1);
                }
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        LC283 alg = new LC283();
        alg.moveZeroes(new int[]{0, 1, 0, 3, 12});
        alg.moveZeroes(new int[]{0});
        System.out.println(alg.backspaceCompare("ab#c", "ad#c"));
        System.out.println(alg.backspaceCompare("ab##", "c#d#"));
        System.out.println(alg.backspaceCompare("y#fo##f", "y#f#o##f"));
    }
}
